<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 15:19
 */

namespace app\logic\index\material;


use app\logic\BaseLogic;
use app\model\material\MaterialCatgoryModel;

class MaterialLogic extends BaseLogic
{
    protected $model;

    protected static $instance;

    public function __construct()
    {
        parent::__construct();
        $this->model = \app\model\material\MaterialDataModel::getInstance();
    }

    public static function getInstance()
    {
//        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return  new self();
    }

    public function getList($param)
    {
        $where = [
            'isdel'=>0,
            'status'=>1,
        ];
        $fields = ['id','name','catid','image','tag','heigh_video_tts','normal_video_tts','show_tts'];

        $list =  $this->model->getWhere($where)->orderBy('id','desc')->get($fields);

        $catMap = MaterialCatgoryModel::getInstance()->getWhere(['status'=>1,'isdel'=>0])->orderBy('sort','desc')->pluck('name','id');

        $categoryDatas = [];
        foreach($catMap as $catid=>$catName){
            foreach($list as $k=>$val){
                //$val['video_tts'] = ceil(($val['heigh_video_tts']+$val['normal_video_tts'])/3600);
                $val['video_tts'] = ceil(($val['heigh_video_tts']+$val['normal_video_tts']));
                if($val['video_tts']==0){
                    continue;
                }

                if($catid==$val['catid']){
                    isset($categoryDatas[$catName])==false && $categoryDatas[$catName]['name'] = $catName;
                    $categoryDatas[$catName]['list'][] = $val;
                }
            }
        }

        //过滤没有素材的分类
        foreach($categoryDatas as $key=>$val){
            if(empty($val['list'])){
                unset($categoryDatas[$key]);
            }
        }

        $data = array_values($categoryDatas);
        return $data;
    }
}